﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;

namespace LifeInSharePoint.SPTwitter.WebParts.NewMessage
{
    [ToolboxItemAttribute(false)]
    public class NewMessage : WebPart
    {

        private static string TwitterList = "SPTwitter";
        private string mentions = string.Empty;
        private string[] messageSplit = null;
        public NewMessage()
        {

        }
        // Child controls
        protected Image img;
        protected TextBox txtMessage;
        protected Button btnPost;
        /// <summary>
        /// Render this Web Part to the output parameter specified.
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        /// 
        protected override void RenderContents(HtmlTextWriter output)
        {

            string SiteCollectionRootUrl = SPContext.Current.Site.RootWeb.Url;

            HtmlLink Jquerylink = new HtmlLink();
            output.Write("<script type='text/javascript' src='" + SiteCollectionRootUrl + "/Style Library/LifeInSharePoint/Javascript/jquery-1.4.2.js'></script>");
            output.Write("<script type='text/javascript' src='" + SiteCollectionRootUrl + "/Style Library/LifeInSharePoint/Javascript/SPTwitter.js'></script>");

            output.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            output.RenderBeginTag("div");

            // Table for the Google image and search scope
            output.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            output.RenderBeginTag("table");
            output.RenderBeginTag("tr");

            // Image cell
            output.AddAttribute(HtmlTextWriterAttribute.Width, "1px");
            output.RenderBeginTag("td");

            output.RenderEndTag(); // td

            // Search scope text cell
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "Left");
            output.RenderBeginTag("td");

            output.RenderEndTag(); // td
            output.RenderEndTag(); // tr
            output.RenderEndTag(); // table

            // Table to display the URL
            output.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            output.RenderBeginTag("table");
            output.RenderBeginTag("tr");
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            output.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, "Arial");
            output.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "8pt");
            output.RenderBeginTag("td");


            output.RenderEndTag(); // td
            output.RenderEndTag(); // tr

            // New table row
            output.RenderBeginTag("tr");
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            output.RenderBeginTag("td");

            // Render search textbox
            this.txtMessage.RenderControl(output);
            output.Write("(Maximum characters: 150)<br/><span class='charLeft'> </span>  Characters left");
            output.Write("<br />");
            // Render search button
            this.btnPost.RenderControl(output);

            output.RenderEndTag(); // td
            output.RenderEndTag(); // tr
            output.RenderEndTag(); // table
            output.RenderEndTag(); // div
        }

        /// <summary>
        /// CreateChildControls is where we need to instantiate all child controls 
        /// within our web part.
        /// </summary>
        protected override void CreateChildControls()
        {
            txtMessage = new TextBox();
            txtMessage.CssClass = "txtMessage";
            txtMessage.Style.Add("WIDTH", "100%");
            txtMessage.MaxLength = 255;
            txtMessage.Text = "";
            txtMessage.TextMode = TextBoxMode.MultiLine;
            txtMessage.Rows = 5;
            Controls.Add(txtMessage);

            btnPost = new Button();
            btnPost.CssClass = "btnPostMessage";
            btnPost.Text = "Post Message";
            btnPost.Font.Size = new FontUnit("8pt");
            btnPost.Click += new EventHandler(btnPost_Click);
            Controls.Add(btnPost);

            base.CreateChildControls();
        }

        private void btnPost_Click(object source, System.EventArgs args)
        {
            if (txtMessage.Text.Length > 0)
            {
                    using (SPWeb web = SPContext.Current.Web)
                    {
                        if (web != null)
                        {
                            web.AllowUnsafeUpdates = true;
                            SPList SPTwitterList = web.Lists[TwitterList];

                            SPListItem SPTwitterListItem = SPTwitterList.Items.Add();

                            SPTwitterListItem["Title"] = txtMessage.Text;
                            messageSplit = txtMessage.Text.Split(' ');
                            foreach (string word in messageSplit)
                            {
                                if (word.StartsWith("@"))
                                {
                                    mentions += word.Replace(',', ' ') + ",";
                                }
                            }
                            SPTwitterListItem["TwitterMentions"] = mentions;
                            SPTwitterListItem.Update();
                        }
                    }
            }
            //After everything - Clear the text
            txtMessage.Text = "";
        }

    }
}

